package com.hp.docker_base.controller;

import com.alibaba.fastjson.JSON;
import com.hp.docker_base.bean.company.CentralSendMsg;
import com.hp.docker_base.bean.company.CentralSendReq;
import com.hp.docker_base.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
@RestController
@RequestMapping("/client")
public class TestController {

    @Autowired
    private TestService testService;
    @PostMapping("record/q")
    public void recordQ() {
        testService.recordQ();
    }


    @PostMapping("ok/v1/devices/batch/control")
    public Map batchControl(@RequestBody List<CentralSendReq> reqs) {
        Map ret = new TreeMap();
        List<CentralSendMsg> retMsgList = new ArrayList<>();
        for (CentralSendReq req : reqs) {
            CentralSendMsg retMsg = new CentralSendMsg();
            BeanUtils.copyProperties(req, retMsg);
            retMsg.setCode(0);

            retMsgList.add(retMsg);
        }
        ret.put("payload", retMsgList);
        return ret;
    }


    @PostMapping("error/v1/devices/batch/control")
    public Map errorBatchControl(@RequestBody List<CentralSendReq> reqs) {
        Map ret = new TreeMap();
        List<CentralSendMsg> retMsgList = new ArrayList<>();
        Integer i = 0;
        for (CentralSendReq req : reqs) {
            CentralSendMsg retMsg = new CentralSendMsg();
            BeanUtils.copyProperties(req, retMsg);

            if ((reqs.size()-1) == i) {
                retMsg.setCode(4);
                retMsg.setMessage("设备离线，该指令将在上线时自动下发");
            } else {
                retMsg.setCode(0);
            }
            retMsgList.add(retMsg);
            i++;
        }
        ret.put("payload", retMsgList);
        return ret;
    }

    @PostMapping("ok/v1/devices/control")
    public CentralSendMsg sendState(@RequestBody  CentralSendReq req) {
        CentralSendMsg retMsg = new CentralSendMsg();
        BeanUtils.copyProperties(req, retMsg);
        retMsg.setCode(0);

        return retMsg;
    }


    @PostMapping("error/v1/devices/control")
    public CentralSendMsg sendErrorState(@RequestBody  CentralSendReq req) {
        CentralSendMsg retMsg = new CentralSendMsg();
        BeanUtils.copyProperties(req, retMsg);
        retMsg.setCode(4);
        retMsg.setMessage("设备离线，该指令将在上线时自动下发");

        return retMsg;
    }

}
